<template>
	<view class="u-page">
		<!-- 顶部标题导航栏 -->
		<u-navbar title="优惠券" :border="false" :fixed="true" :placeholder="true" bgColor="#2979ff"
			titleStyle="color: #ffffff; font-size: 32rpx;" :height="44" leftIcon=""></u-navbar>

		<!-- 状态切换导航 -->
		<view class="tab-bar">
			<view v-for="(item, index) in tabs" :key="index" class="tab-item"
				:class="{ active: userCouponData.state === index }" @click="switchTab(index)">
				{{ item }}
			</view>
		</view>

		<view v-for="(item,index) in userCouponList" :key="index" class="u-page__content">
			<!-- 待使用列表 -->
			<view class="coupon-list" v-if="item.state === 0">
				<view class="coupon-item">
					<view class="coupon-content">
						<view class="left">
							<view class="title">仅限于{{item.cheCoupon.cheShop.shopName}}使用</view>
							<view class="condition">满{{item.cheCoupon.full}}元可用</view>
							<view class="date">有效期至：{{item.cheCoupon.useTime}}—{{item.cheCoupon.useendTime}}</view>
						</view>
						<view class="right">
							<text class="amount">¥<text class="num">{{item.cheCoupon.reduce}}</text></text>
							<view class="use-btn">去使用</view>
						</view>
					</view>
				</view>
			</view>

			<!-- 已使用列表 -->
			<view class="coupon-list" v-if="item.state === 1">
				<view class="coupon-item used">
					<view class="coupon-info">
						<text>仅限于{{item.cheCoupon.cheShop.shopName}}使用</text>
						<text>满{{item.cheCoupon.full}}元可用</text>
					</view>
					<view class="coupon-amount">
						<text>¥</text>
						<text class="amount">{{item.cheCoupon.reduce}}</text>
					</view>
					<view class="coupon-bottom">
						<text class="date">有效期至：{{item.cheCoupon.useTime}}—{{item.cheCoupon.useendTime}}</text>
						<text class="status">已使用 ></text>
					</view>
				</view>
			</view>

			<!-- 已过期列表 -->
			<view class="coupon-list" v-if="item.state === 2">
				<view class="coupon-item expired">
					<view class="coupon-info">
						<text>仅限于{{item.cheCoupon.cheShop.shopName}}使用</text>
						<text>满{{item.cheCoupon.full}}元可用</text>
					</view>
					<view class="coupon-amount">
						<text>¥</text>
						<text class="amount">{{item.cheCoupon.reduce}}</text>
					</view>
					<view class="coupon-bottom">
						<text class="date">{{item.cheCoupon.useTime}}—{{item.cheCoupon.useendTime}}</text>
						<text class="status">已过期 ></text>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				tabs: ['待使用', '已使用', '已过期'],
				userCouponList: [],
				userCouponData: {
					state: 0,
					userid:''
				}
			}
		},
		onShow() {
			this.getList()
		},
		onLoad() {
			const Id = this.getCookie('userid')
			this.userCouponData.userid=Id
		},
		methods: {
			getCookie(name) {
				const nameEQ = name + "=";
				const cookies = document.cookie.split(';');
				for (let i = 0; i < cookies.length; i++) {
					let c = cookies[i].trim();
					if (c.indexOf(nameEQ) === 0) {
						return decodeURIComponent(c.substring(nameEQ.length));
					}
				}
				return null;
			},
			switchTab(index) {
				this.userCouponData.state=index
				this.getList()
			},
			getList() {
				let that = this
				this.$http({
					url: '/api/user/coupon_user/list',
					method: 'get',
					data: that.userCouponData,
					success(res) {
						that.userCouponList = res.data.rows
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.tab-bar {
		display: flex;
		background: #2979ff;
		padding: 20rpx 0;
		position: fixed;
		top: 44px; // 导航栏高度
		left: 0;
		right: 0;
		z-index: 99;

		.tab-item {
			flex: 1;
			text-align: center;
			color: rgba(255, 255, 255, 0.8);
			font-size: 28rpx;

			&.active {
				color: #ffffff;
				font-weight: bold;
			}
		}
	}

	.u-page__content {
		padding: 20rpx;
		background-color: #f5f5f5;
		margin-top: 132rpx; // 导航栏 + tab栏高度
	}

	/* 待使用样式 */
	.coupon-list {
		.coupon-item {
			margin-bottom: 20rpx;
			background: #ffffff;
			border-radius: 12rpx;
			overflow: hidden;

			.coupon-content {
				background: linear-gradient(90deg, #ff6b6b, #ff9f43);
				margin: 2rpx;
				border-radius: 10rpx;
				padding: 30rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;
				position: relative;

				&::before {
					content: '';
					position: absolute;
					left: -10rpx;
					top: 50%;
					width: 20rpx;
					height: 20rpx;
					border-radius: 50%;
					background: #f5f5f5;
					transform: translateY(-50%);
				}

				&::after {
					content: '';
					position: absolute;
					right: -10rpx;
					top: 50%;
					width: 20rpx;
					height: 20rpx;
					border-radius: 50%;
					background: #f5f5f5;
					transform: translateY(-50%);
				}

				.left {
					flex: 1;
					color: #ffffff;

					.title {
						font-size: 28rpx;
						margin-bottom: 10rpx;
					}

					.condition {
						font-size: 24rpx;
						margin-bottom: 10rpx;
					}

					.date {
						font-size: 24rpx;
						opacity: 0.8;
					}
				}

				.right {
					text-align: center;
					padding-left: 30rpx;
					position: relative;

					&::before {
						content: '';
						position: absolute;
						left: 0;
						top: -30rpx;
						bottom: -30rpx;
						width: 1px;
						background: rgba(255, 255, 255, 0.3);
						border-left: 1px dashed rgba(255, 255, 255, 0.3);
					}

					.amount {
						color: #ffffff;
						font-size: 28rpx;

						.num {
							font-size: 56rpx;
							font-weight: bold;
						}
					}

					.use-btn {
						color: #ffffff;
						font-size: 24rpx;
						margin-top: 10rpx;
					}
				}
			}
		}
	}

	/* 已使用样式 */
	.coupon-item.used {
		background: #FFB8A1;
		margin-bottom: 20rpx;
		border-radius: 12rpx;
		padding: 20rpx;
		position: relative;

		// 左侧圆形缺口
		&::before {
			content: '';
			position: absolute;
			left: -10rpx;
			top: 50%;
			transform: translateY(-50%);
			width: 20rpx;
			height: 20rpx;
			background: #f5f5f5;
			border-radius: 50%;
		}

		// 右侧圆形缺口
		&::after {
			content: '';
			position: absolute;
			right: -10rpx;
			top: 50%;
			transform: translateY(-50%);
			width: 20rpx;
			height: 20rpx;
			background: #f5f5f5;
			border-radius: 50%;
		}

		.coupon-info {
			display: flex;
			flex-direction: column;
			gap: 10rpx;
			color: #ffffff;
			font-size: 28rpx;
		}

		.coupon-amount {
			text-align: right;
			color: #ffffff;
			margin: 10rpx 0;

			.amount {
				font-size: 56rpx;
				font-weight: bold;
				margin-left: 4rpx;
			}
		}

		.coupon-bottom {
			display: flex;
			justify-content: space-between;
			align-items: center;
			color: #ffffff;
			font-size: 24rpx;

			.date {
				opacity: 0.8;
			}

			.status {
				font-size: 24rpx;
			}
		}
	}

	/* 已过期样式 */
	.coupon-item.expired {
		background: #CCCCCC;
		margin-bottom: 20rpx;
		border-radius: 12rpx;
		padding: 20rpx;
		position: relative;

		// 左侧圆形缺口
		&::before {
			content: '';
			position: absolute;
			left: -10rpx;
			top: 50%;
			transform: translateY(-50%);
			width: 20rpx;
			height: 20rpx;
			background: #f5f5f5;
			border-radius: 50%;
		}

		// 右侧圆形缺口
		&::after {
			content: '';
			position: absolute;
			right: -10rpx;
			top: 50%;
			transform: translateY(-50%);
			width: 20rpx;
			height: 20rpx;
			background: #f5f5f5;
			border-radius: 50%;
		}

		.coupon-info {
			display: flex;
			flex-direction: column;
			gap: 10rpx;
			color: #ffffff;
			font-size: 28rpx;
		}

		.coupon-amount {
			text-align: right;
			color: #ffffff;
			margin: 10rpx 0;

			.amount {
				font-size: 56rpx;
				font-weight: bold;
				margin-left: 4rpx;
			}
		}

		.coupon-bottom {
			display: flex;
			justify-content: space-between;
			align-items: center;
			color: #ffffff;
			font-size: 24rpx;

			.date {
				opacity: 0.8;
			}

			.status {
				font-size: 24rpx;
			}
		}
	}
</style>